package cm.commom;

import java.util.Objects;

public class ListNode {
    public int val;
    public ListNode next = null;
    public ListNode(int val) {
        this.val = val;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ListNode node = (ListNode) o;
        return val == node.val && Objects.equals(next, node.next);
    }

    @Override
    public int hashCode() {
        return Objects.hash(val, next);
    }

    public static boolean is2Delete(ListNode node){
        return node.next != null && node.val == node.next.val;
    }

    public static void deleteNode(ListNode prePtr,ListNode node){
        while (node.next!=null && node.val==node.next.val){
            node = node.next;
        }
        prePtr.next = node.next;
    }

    public static ListNode buildList(int []nodes){
        if (nodes==null){
            return null;
        }
        if (nodes.length==1){
            return new ListNode(nodes[0]);
        }

        ListNode head = new ListNode( nodes[nodes.length-1] );
        ListNode listNode;

        for (int i = nodes.length-2; i >= 0 ; i--) {
            listNode = new ListNode(nodes[i]);
            listNode.next = head;
            head = listNode;
        }
        return head;
    }

    public static String getListString(ListNode head){
        ListNode node = head;
        String result = "";
        while (node.next!=null){
            result += node.val + " ";
            node = node.next;
        }
        result += node.val;
        return result;
    }
}
